package cn.lxs.web.Cookie;

import javax.lang.model.util.SimpleElementVisitor6;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 登录状态用
 */
@WebServlet("/cookietest")
public class CookieTest extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");

        //获取所有的Cookie
        Cookie[] cookies = req.getCookies();
        Boolean flag = true;
        //遍历查看是否有lasttime
        if(cookies!=null&&cookies.length>0){
            for (Cookie cookie : cookies) {
                //获取所有的名称
                String name = cookie.getName();
                if("lastTime".equals(name))
                {
                    flag = false;
                    //有该cookie就不是第一次访问
                    String value = cookie.getValue();
                    //获取当前时间
                    Date date = new Date();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 - HH:mm:ss");
                    String str_date = sdf.format(date);
                    //因为cookie无法存放特殊字符
                    //需要重新编码

                    str_date = URLEncoder.encode(str_date,"utf-8");
                    cookie.setValue(str_date);
                    //设置cookie的存活时间
                    cookie.setMaxAge(60*60*24*30);
                    //设置保存一个月
                    resp.addCookie(cookie);
                    //将value反解码
                    value = URLDecoder.decode(value,"utf-8");

                    resp.getWriter().print("<script>" +
                            "alert('欢迎回来,您上次访问的时间为"+value+"');" +
                            "</script>");
                    break;
                }
            }
        }
        if(cookies==null||cookies.length==0||flag)
        {
            //表示没有第一次访问
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 - HH:mm:ss");
            //记住cookie不能直接打空格

            String str_date = sdf.format(date);
            str_date = URLEncoder.encode(str_date,"utf-8");
            //将时间数据重新编码
            Cookie cookie = new Cookie("lastTime", str_date );
            cookie.setValue(str_date);
            //设置cookie的存活时间
            cookie.setMaxAge(60*60*24*30);
            //设置保存一个月
            resp.addCookie(cookie);
            resp.getWriter().print("<script>" +
                    "alert('欢迎您,首次登录我们网站');" +
                    "</script>");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }
}
